#ifndef __CUSTOMDIALOG_MQH__
#define __CUSTOMDIALOG_MQH__

#include <Controls\Dialog.mqh>
#include <Controls\Label.mqh>
#include <Controls\Button.mqh>
#include <Controls\Edit.mqh>

// 基础对话框类，支持事件映射
class CAppDialogEx : public CDialog
{
public:
    CAppDialogEx() : CDialog() {}
    virtual ~CAppDialogEx() {}

    // 事件映射机制
    virtual bool OnEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
    {
        // 先尝试事件映射
        if(EventMap(id, lparam, dparam, sparam))
            return true;
        // 默认行为
        return CDialog::OnEvent(id, lparam, dparam, sparam);
    }

    // 重写Create方法以确保面板正确显示
    virtual bool Create(const long chart, const string name, const int subwin, const int x1, const int y1, const int x2, const int y2)
    {
        if(!CDialog::Create(chart, name, subwin, x1, y1, x2, y2))
            return false;
            
        // 设置面板属性
        ObjectSetInteger(chart, name, OBJPROP_SELECTABLE, false);
        ObjectSetInteger(chart, name, OBJPROP_SELECTED, false);
        ObjectSetInteger(chart, name, OBJPROP_HIDDEN, true);
        ObjectSetInteger(chart, name, OBJPROP_ZORDER, 1000);
        ObjectSetInteger(chart, name, OBJPROP_BACK, false);
        ObjectSetInteger(chart, name, OBJPROP_READONLY, true);
        ObjectSetInteger(chart, name, OBJPROP_CORNER, CORNER_LEFT_UPPER);
        ObjectSetInteger(chart, name, OBJPROP_ANCHOR, ANCHOR_LEFT_UPPER);
        ObjectSetInteger(chart, name, OBJPROP_XDISTANCE, x1);
        ObjectSetInteger(chart, name, OBJPROP_YDISTANCE, y1);
        ObjectSetInteger(chart, name, OBJPROP_XSIZE, x2-x1);
        ObjectSetInteger(chart, name, OBJPROP_YSIZE, y2-y1);
        ObjectSetInteger(chart, name, OBJPROP_BGCOLOR, clrWhite);
        ObjectSetInteger(chart, name, OBJPROP_BORDER_TYPE, BORDER_FLAT);
        ObjectSetInteger(chart, name, OBJPROP_COLOR, clrBlack);
        ObjectSetInteger(chart, name, OBJPROP_STYLE, STYLE_SOLID);
        ObjectSetInteger(chart, name, OBJPROP_WIDTH, 1);
        ObjectSetInteger(chart, name, OBJPROP_FILL, true);
        
        return true;
    }

    // 重写Show方法以防止面板意外隐藏
    virtual bool Show()
    {
        if(!CDialog::Show())
            return false;
            
        // 确保面板始终可见
        ObjectSetInteger(m_chart_id, m_name, OBJPROP_HIDDEN, false);
        ObjectSetInteger(m_chart_id, m_name, OBJPROP_ZORDER, 1000);
        
        // 强制更新面板位置
        ForcePanelOnTop();
        
        return true;
    }

    // 重写Hide方法以防止意外隐藏
    virtual bool Hide()
    {
        // 不允许直接隐藏面板
        return false;
    }

    // 重写BringToTop方法以确保面板始终在最上层
    virtual void BringToTop()
    {
        ObjectSetInteger(m_chart_id, m_name, OBJPROP_ZORDER, 1000);
        ObjectSetInteger(m_chart_id, m_name, OBJPROP_HIDDEN, false);
        
        // 强制更新面板位置
        ForcePanelOnTop();
    }

    // 添加新方法强制面板在最上层
    void ForcePanelOnTop()
    {
        // 设置面板属性
        ObjectSetInteger(m_chart_id, m_name, OBJPROP_SELECTABLE, false);
        ObjectSetInteger(m_chart_id, m_name, OBJPROP_SELECTED, false);
        ObjectSetInteger(m_chart_id, m_name, OBJPROP_HIDDEN, false);
        ObjectSetInteger(m_chart_id, m_name, OBJPROP_ZORDER, 1000);
        ObjectSetInteger(m_chart_id, m_name, OBJPROP_BACK, false);
        ObjectSetInteger(m_chart_id, m_name, OBJPROP_READONLY, true);
        ObjectSetInteger(m_chart_id, m_name, OBJPROP_CORNER, CORNER_LEFT_UPPER);
        ObjectSetInteger(m_chart_id, m_name, OBJPROP_ANCHOR, ANCHOR_LEFT_UPPER);
        
        // 强制重绘图表
        ChartRedraw(m_chart_id);
    }

protected:
    // 可扩展事件映射钩子
    virtual bool EventMap(const int id, const long &lparam, const double &dparam, const string &sparam)
    {
        // 默认无事件映射，子类可重载
        return false;
    }
};

// 自定义对话框类，继承自CAppDialogEx
class CCustomDialog : public CAppDialogEx
{
protected:
    bool   m_minimized;
    CRect  m_rect, m_norm_rect, m_min_rect;
    string m_name;
    long   m_chart_id;
    int    m_subwin;
    double m_DPIScale;

    // 面板参数
    int m_panel_w;
    int m_label_h;
    int m_edit_w;
    int m_btn_w;
    int m_btn_h;
    int m_gap_y;
    int m_gap_x;
    int m_font_size;

public:
    CCustomDialog() : m_minimized(false), m_chart_id(0), m_subwin(0), m_DPIScale(1.0) 
    {
        // 初始化面板参数
        m_panel_w = 530;
        m_label_h = 24;
        m_edit_w = 120;
        m_btn_w = 120;
        m_btn_h = 32;
        m_gap_y = 12;
        m_gap_x = 14;
        m_font_size = 8;
    }
    
    virtual ~CCustomDialog() {}

    // 创建面板
    virtual bool Create(const long chart, const string name, const int subwin, const int x1, const int y1, const int x2, const int y2)
    {
        m_chart_id = chart;
        m_name = name;
        m_subwin = subwin;
        double screen_dpi = (double)TerminalInfoInteger(TERMINAL_SCREEN_DPI);
        m_DPIScale = screen_dpi / 96.0;
        
        if(!CAppDialogEx::Create(chart, name, subwin, x1, y1, x2, y2))
            return false;
            
        // 设置面板属性
        ObjectSetInteger(chart, name, OBJPROP_SELECTABLE, false);
        ObjectSetInteger(chart, name, OBJPROP_SELECTED, false);
        ObjectSetInteger(chart, name, OBJPROP_HIDDEN, true);
        ObjectSetInteger(chart, name, OBJPROP_ZORDER, 1000);
        ObjectSetInteger(chart, name, OBJPROP_BACK, false);
        ObjectSetInteger(chart, name, OBJPROP_READONLY, true);
        ObjectSetInteger(chart, name, OBJPROP_CORNER, CORNER_LEFT_UPPER);
        ObjectSetInteger(chart, name, OBJPROP_ANCHOR, ANCHOR_LEFT_UPPER);
        ObjectSetInteger(chart, name, OBJPROP_XDISTANCE, x1);
        ObjectSetInteger(chart, name, OBJPROP_YDISTANCE, y1);
        ObjectSetInteger(chart, name, OBJPROP_XSIZE, x2-x1);
        ObjectSetInteger(chart, name, OBJPROP_YSIZE, y2-y1);
        ObjectSetInteger(chart, name, OBJPROP_BGCOLOR, clrWhite);
        ObjectSetInteger(chart, name, OBJPROP_BORDER_TYPE, BORDER_FLAT);
        ObjectSetInteger(chart, name, OBJPROP_COLOR, clrBlack);
        ObjectSetInteger(chart, name, OBJPROP_STYLE, STYLE_SOLID);
        ObjectSetInteger(chart, name, OBJPROP_WIDTH, 1);
        ObjectSetInteger(chart, name, OBJPROP_FILL, true);
        ObjectSetInteger(chart, name, OBJPROP_SELECTABLE, false);
        ObjectSetInteger(chart, name, OBJPROP_SELECTED, false);
        ObjectSetInteger(chart, name, OBJPROP_HIDDEN, true);
        ObjectSetInteger(chart, name, OBJPROP_ZORDER, 1000);
        
        m_rect.SetBound(x1, y1, x2, y2);
        m_norm_rect.SetBound(x1, y1, x2, y2);
        m_min_rect.SetBound(x1, y1, x1 + 100, y1 + 20);
        
        // 创建面板控件
        return CreateControls();
    }

    // 创建面板控件
    virtual bool CreateControls()
    {
        int cur_y = 40; // 确保不遮挡标题栏
        int xx1 = 20, xx2 = m_panel_w - 20;
        
        // 展示区
        if(!CreateDisplayArea(xx1, xx2, cur_y)) return false;
        
        // 修改区
        if(!CreateModifyArea(xx1, xx2, cur_y)) return false;
        
        // 操作区
        if(!CreateOperationArea(xx1, xx2, cur_y)) return false;
        
        return true;
    }

    // 创建展示区
    virtual bool CreateDisplayArea(int xx1, int xx2, int &cur_y)
    {
        int yy1 = cur_y, yy2 = yy1 + m_label_h;
        
        // 总盈亏和锁单收益同一行
        int half_width = (xx2 - xx1) / 2;
        if(!lbl_totalprofit.Create(m_chart_id, m_name+"_totalprofit", m_subwin, xx1, yy1, xx1+half_width, yy2)) return false;
        lbl_totalprofit.FontSize(m_font_size + 2);
        if(!Add(lbl_totalprofit)) return false;
        if(!lbl_lockprofit.Create(m_chart_id, m_name+"_lockprofit", m_subwin, xx1+half_width, yy1, xx2, yy2)) return false;
        lbl_lockprofit.FontSize(m_font_size + 2);
        if(!Add(lbl_lockprofit)) return false;
        cur_y = yy2 + m_gap_y;
        
        // 买入盈亏
        yy1 = cur_y; yy2 = yy1 + m_label_h;
        if(!lbl_buyinfo.Create(m_chart_id, m_name+"_buyinfo", m_subwin, xx1, yy1, xx2, yy2)) return false;
        lbl_buyinfo.FontSize(m_font_size + 2);
        if(!Add(lbl_buyinfo)) return false;
        cur_y = yy2 + m_gap_y;
        
        // 卖出盈亏
        yy1 = cur_y; yy2 = yy1 + m_label_h;
        if(!lbl_sellinfo.Create(m_chart_id, m_name+"_sellinfo", m_subwin, xx1, yy1, xx2, yy2)) return false;
        lbl_sellinfo.FontSize(m_font_size + 2);
        if(!Add(lbl_sellinfo)) return false;
        cur_y = yy2 + m_gap_y;

        // 状态显示
        yy1 = cur_y; yy2 = yy1 + m_label_h;
        if(!lbl_status.Create(m_chart_id, m_name+"_status", m_subwin, xx1, yy1, xx2, yy2)) return false;
        lbl_status.FontSize(m_font_size + 2);
        if(!Add(lbl_status)) return false;
        cur_y = yy2 + m_gap_y;
        
        // 分割线
        yy1 = cur_y; yy2 = yy1 + 8;
        if(!lbl_sep1.Create(m_chart_id, m_name+"_sep1", m_subwin, xx1, yy1, xx2, yy2)) return false;
        lbl_sep1.Text("----------------------------------------------------------------------------------------------------");
        if(!Add(lbl_sep1)) return false;
        cur_y = yy2 + m_gap_y * 2;
        
        return true;
    }

    // 创建修改区
    virtual bool CreateModifyArea(int xx1, int xx2, int &cur_y)
    {
        int param_col1 = xx1, param_col2 = param_col1 + 90;
        int param_col3 = param_col2 + m_edit_w + m_gap_x, param_col4 = param_col3 + 90;
        
        // 当前倍率
        int yy1 = cur_y, yy2 = yy1 + m_label_h;
        if(!CreateModifyRow(param_col1, param_col2, param_col3, param_col4, yy1, yy2,
                           "当前倍率:", lbl_multi, edit_multiplier,
                           "加仓ATR:", lbl_atr, edit_atrrange)) return false;
        cur_y = yy2 + m_gap_y;
        
        // 止盈参数
        yy1 = cur_y; yy2 = yy1 + m_label_h;
        if(!lbl_sp.Create(m_chart_id, m_name+"_lblsp", m_subwin, param_col1, yy1, param_col2, yy2)) return false;
        lbl_sp.Text("止盈参数:");
        if(!Add(lbl_sp)) return false;
        if(!edit_stopprofit.Create(m_chart_id, m_name+"_editsp", m_subwin, param_col2, yy1, param_col2+m_edit_w, yy2)) return false;
        if(!Add(edit_stopprofit)) return false;
        cur_y = yy2 + m_gap_y;
        
        // 初始仓位和最大订单数
        yy1 = cur_y; yy2 = yy1 + m_label_h;
        if(!CreateModifyRow(param_col1, param_col2, param_col3, param_col4, yy1, yy2,
                           "初始仓位:", lbl_initial_lot, edit_initial_lot,
                           "最大订单:", lbl_max_order_count, edit_max_order_count)) return false;
        cur_y = yy2 + m_gap_y;
        
        // 修改按钮
        yy1 = cur_y; yy2 = yy1 + m_btn_h;
        int btn_modify_x = xx1 + (m_panel_w-2*xx1-m_btn_w)/2;
        if(!btn_modify.Create(m_chart_id, m_name+"_modify", m_subwin, btn_modify_x, yy1, btn_modify_x+m_btn_w, yy2)) return false;
        btn_modify.Text("修改参数");
        if(!Add(btn_modify)) return false;
        
        // 添加重置按钮到修改参数按钮旁边
        if(!btn_reset.Create(m_chart_id, m_name+"_reset", m_subwin, btn_modify_x+m_btn_w+m_gap_x, yy1, btn_modify_x+2*m_btn_w+m_gap_x, yy2)) return false;
        btn_reset.Text("重置");
        btn_reset.Color(clrOrange);
        if(!Add(btn_reset)) return false;
        
        cur_y = yy2 + m_gap_y * 1;
        
        // 分割线
        yy1 = cur_y; yy2 = yy1 + 8;
        if(!lbl_sep2.Create(m_chart_id, m_name+"_sep2", m_subwin, xx1, yy1, xx2, yy2)) return false;
        lbl_sep2.Text("----------------------------------------------------------------------------------------------------");
        if(!Add(lbl_sep2)) return false;
        cur_y = yy2 + m_gap_y * 1;
        
        return true;
    }

    // 创建操作区
    virtual bool CreateOperationArea(int xx1, int xx2, int &cur_y)
    {
        int op_col1 = xx1, op_col2 = op_col1 + m_btn_w + m_gap_x;
        int op_col3 = op_col2 + m_btn_w + m_gap_x;  // 添加第三列

        // 平仓按钮
        int yy1 = cur_y, yy2 = yy1 + m_btn_h;
        if(!btn_close_buy.Create(m_chart_id, m_name+"_close_buy", m_subwin, op_col1, yy1, op_col1+m_btn_w, yy2)) return false;
        btn_close_buy.Text("平买单");
        btn_close_buy.Color(clrGreen);
        if(!Add(btn_close_buy)) return false;
        
        if(!btn_close_sell.Create(m_chart_id, m_name+"_close_sell", m_subwin, op_col2, yy1, op_col2+m_btn_w, yy2)) return false;
        btn_close_sell.Text("平卖单");
        btn_close_sell.Color(clrRed);
        if(!Add(btn_close_sell)) return false;

        // 添加锁单按钮到第三列
        if(!btn_lock.Create(m_chart_id, m_name+"_lock", m_subwin, op_col3, yy1, op_col3+m_btn_w, yy2)) return false;
        btn_lock.Text("锁单");
        btn_lock.Color(clrRed);
        if(!Add(btn_lock)) return false;

        // 添加解锁按钮到第三列（默认隐藏）
        if(!btn_unlock.Create(m_chart_id, m_name+"_unlock", m_subwin, op_col3, yy1, op_col3+m_btn_w, yy2)) return false;
        btn_unlock.Text("解锁");
        btn_unlock.Color(clrBlue);
        btn_unlock.Visible(false);  // 默认隐藏解锁按钮
        if(!Add(btn_unlock)) return false;

        cur_y = yy2 + m_gap_y;
        
        // 分割线
        yy1 = cur_y; yy2 = yy1 + 8;
        if(!lbl_sep3.Create(m_chart_id, m_name+"_sep3", m_subwin, xx1, yy1, xx2, yy2)) return false;
        lbl_sep3.Text("----------------------------------------------------------------------------------------------------");
        if(!Add(lbl_sep3)) return false;
        cur_y = yy2 + m_gap_y * 2;
        
        // 加仓按钮和输入框
        yy1 = cur_y; yy2 = yy1 + m_btn_h;
        if(!btn_add_buy.Create(m_chart_id, m_name+"_add_buy", m_subwin, op_col1, yy1, op_col1+m_btn_w, yy2)) return false;
        btn_add_buy.Text("再买一单");
        btn_add_buy.Color(clrGreen);
        if(!Add(btn_add_buy)) return false;
        
        if(!edit_next_buy_lot.Create(m_chart_id, m_name+"_editnextbuy", m_subwin, op_col2, yy1, op_col2+m_edit_w, yy2)) return false;
        if(!Add(edit_next_buy_lot)) return false;
        cur_y = yy2 + m_gap_y;
        
        yy1 = cur_y; yy2 = yy1 + m_btn_h;
        if(!btn_add_sell.Create(m_chart_id, m_name+"_add_sell", m_subwin, op_col1, yy1, op_col1+m_btn_w, yy2)) return false;
        btn_add_sell.Text("再卖一单");
        btn_add_sell.Color(clrRed);
        if(!Add(btn_add_sell)) return false;
        
        if(!edit_next_sell_lot.Create(m_chart_id, m_name+"_editnextsell", m_subwin, op_col2, yy1, op_col2+m_edit_w, yy2)) return false;
        if(!Add(edit_next_sell_lot)) return false;
        cur_y = yy2 + m_gap_y;

        // 分割线
        yy1 = cur_y; yy2 = yy1 + 8;
        if(!lbl_sep4.Create(m_chart_id, m_name+"_sep4", m_subwin, xx1, yy1, xx2, yy2)) return false;
        lbl_sep4.Text("----------------------------------------------------------------------------------------------------");
        if(!Add(lbl_sep4)) return false;
        cur_y = yy2 + m_gap_y * 1.5;

        return true;
    }

    // 创建修改区的一行控件
    virtual bool CreateModifyRow(int col1, int col2, int col3, int col4, int yy1, int yy2,
                                string label1, CLabel &lbl1, CEdit &edit1,
                                string label2, CLabel &lbl2, CEdit &edit2)
    {
        if(!lbl1.Create(m_chart_id, m_name+"_lbl"+label1, m_subwin, col1, yy1, col2, yy2)) return false;
        lbl1.Text(label1);
        if(!Add(lbl1)) return false;
        
        if(!edit1.Create(m_chart_id, m_name+"_edit"+label1, m_subwin, col2, yy1, col2+m_edit_w, yy2)) return false;
        if(!Add(edit1)) return false;
        
        if(!lbl2.Create(m_chart_id, m_name+"_lbl"+label2, m_subwin, col3, yy1, col4, yy2)) return false;
        lbl2.Text(label2);
        if(!Add(lbl2)) return false;
        
        if(!edit2.Create(m_chart_id, m_name+"_edit"+label2, m_subwin, col4, yy1, col4+m_edit_w, yy2)) return false;
        if(!Add(edit2)) return false;
        
        return true;
    }

    // 刷新面板数据
    virtual void Refresh()
    {
        // 子类需要实现具体的刷新逻辑
    }

    // 最小化/最大化
    virtual void Minimize()
    {
        if(!m_minimized)
        {
            m_minimized = true;
            Move(m_min_rect.left, m_min_rect.top);
            m_rect.SetBound(m_min_rect);
        }
    }
    
    virtual void Maximize()
    {
        if(m_minimized)
        {
            m_minimized = false;
            Move(m_norm_rect.left, m_norm_rect.top);
            m_rect.SetBound(m_norm_rect);
        }
    }
    
    virtual bool IsMinimized() { return m_minimized; }
    
    virtual void BringToTop()
    {
        ObjectSetInteger(m_chart_id, m_name, OBJPROP_ZORDER, 1000);
    }

    // 设置字体大小
    void SetFontSize(int size)
    {
        m_font_size = size;
    }

protected:
    // 面板控件
    CLabel lbl_totalprofit, lbl_buyinfo, lbl_sellinfo;
    CLabel lbl_sep1, lbl_sep2, lbl_sep3, lbl_sep4;
    CLabel lbl_multi, lbl_atr, lbl_sp, lbl_initial_lot, lbl_max_order_count;
    CLabel lbl_status;  // 添加状态标签
    CEdit edit_multiplier, edit_atrrange, edit_stopprofit, edit_initial_lot, edit_max_order_count;
    CButton btn_modify;
    CButton btn_close_buy, btn_close_sell, btn_add_buy, btn_add_sell;
    CButton btn_lock, btn_unlock;
    CEdit edit_next_buy_lot, edit_next_sell_lot;
    CButton btn_reset;
    CLabel lbl_lockprofit;
};

#endif // __CUSTOMDIALOG_MQH__ 